package br.com.tiespec.ejb.Dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import br.com.tiespec.model.Enum.TipoSortOrder;
import br.com.tiespec.model.Modelo.Artefato;
import br.com.tiespec.model.Util.DAOException;

@Stateless(mappedName = "ArtefatoDAO")
public class ArtefatoDAO extends DAO {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public List<Artefato> listarArtefato(int first, int pageSize, String sortField,
			TipoSortOrder sortOrder, Long idClientePortal, String nome, String projeto, String descricao) {
		List<Artefato> aList = new ArrayList<Artefato>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(Artefato.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.setFetchMode("projeto", FetchMode.JOIN)
					.setFetchMode("responsavel", FetchMode.JOIN);
			
			if (idClientePortal != null)
				criteria.add(Restrictions.eq("cliente.id", idClientePortal));
			if (isNullOrBlank(nome))
				criteria.add(Restrictions.ilike("nome", "%" + nome + "%"));
			if (isNullOrBlank(descricao))
				criteria.add(Restrictions.ilike("nome", "%" + nome + "%"));
			if (isNullOrBlank(projeto))
				criteria.add(Restrictions.ilike("projeto.nome", "%" + nome + "%"));
			
			
			criteria = montaPaginacaoSimples(criteria, first, pageSize, sortField, sortOrder);
			
			aList = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return aList;
	}

	public Integer countArtefato(Long idClientePortal, String nome, String projeto, String descricao) {
		Integer count = 0;
		try {
			begin();
			Criteria criteria = this.session.createCriteria(Artefato.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			
			if (idClientePortal != null)
				criteria.add(Restrictions.eq("cliente.id", idClientePortal));
			if (isNullOrBlank(nome))
				criteria.add(Restrictions.ilike("nome", "%" + nome + "%"));
			if (isNullOrBlank(descricao))
				criteria.add(Restrictions.ilike("nome", "%" + nome + "%"));
			if (isNullOrBlank(projeto))
				criteria.add(Restrictions.ilike("projeto.nome", "%" + nome + "%"));
			
			criteria.setProjection(Projections.rowCount());
			
			count = Integer.parseInt(criteria.uniqueResult().toString());
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return count;
	}

	public Artefato selecionar(Long id) {
		Artefato aInstance = new Artefato();
		try {
			begin();
			aInstance = (Artefato) this.session.createCriteria(Artefato.class)
					.add(Restrictions.eq("id", id)).uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return aInstance;
	}

	public void remover(Long id) throws DAOException {
		delete(Artefato.class, id);
	}

	public Artefato selecionarCompleto(Long id) {
		Artefato aInstance = new Artefato();
		try {
			begin();
			aInstance = (Artefato) this.session.createCriteria(Artefato.class)
					.add(Restrictions.eq("id", id))
					.setFetchMode("responsavel", FetchMode.JOIN)
					.setFetchMode("projeto", FetchMode.JOIN)
					.setFetchMode("anexos", FetchMode.JOIN)
					.setFetchMode("documentos", FetchMode.JOIN)
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return aInstance;
	}

	public Artefato selecionarComUsuarioEProjeto(Long id) {
		Artefato aInstance = new Artefato();
		try {
			begin();
			aInstance = (Artefato) this.session.createCriteria(Artefato.class)
					.add(Restrictions.eq("id", id))
					.setFetchMode("responsavel", FetchMode.JOIN)
					.setFetchMode("projeto", FetchMode.JOIN)
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return aInstance;
	}
	
}